/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aal.business;

import com.aal.bean.CustomerFacadeLocal;
import com.aal.bean.CustomerGroupFacadeLocal;
import com.aal.entity.Customer;
import com.aal.entity.CustomerGroup;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

/**
 *
 * @author quy
 */
@Stateless
public class CustomerManagement implements CustomerManagementLocal {
    
    @EJB
    private CustomerGroupFacadeLocal customerGroupFacade;
    @EJB
    private CustomerFacadeLocal customerFacade;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    public boolean createCustomerGroup(double discountpercent) {
        try {
            CustomerGroup customerGroup = new CustomerGroup();
            customerGroup.setCustomerGroupID(Integer.MIN_VALUE);
            customerGroup.setDiscountPercent(discountpercent);
            customerGroupFacade.create(customerGroup);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    
    public boolean editCustomerGroup(int customergroupId, double discountpercent) {
        try {
            CustomerGroup customergroup = customerGroupFacade.find(customergroupId);
            customergroup.setDiscountPercent(discountpercent);
            customerGroupFacade.edit(customergroup);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    @Interceptors(PasswordInterceptor.class)
    public boolean createCustomer(String username, String password, int customergroupId, String firstname, String lastname, String email, String phone, String billingaddress) {
        try {
            Customer customer = new Customer();
            customer.setUsername(username);
            customer.setPassword(password);
            customer.setCustomerGroupID(customerGroupFacade.find(customergroupId));
            customer.setFirstname(firstname);
            customer.setLastname(lastname);
            customer.setEmail(email);
            customer.setPhone(phone);
            customer.setBillingAddress(billingaddress);
            customerFacade.create(customer);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    @Interceptors(PasswordInterceptor.class)
    public boolean editCustomer(String username, String password, int customergroupId, String firstname, String lastname, String email, String phone, String billingaddress) {
        try {
            Customer customer = customerFacade.find(username);
            if (password != null) {
                customer.setPassword(password);
            }
            customer.setCustomerGroupID(customerGroupFacade.find(customergroupId));
            if (firstname != null) {
                customer.setFirstname(firstname);
            }
            if (lastname != null) {
                customer.setLastname(lastname);
            }
            if (email != null) {
                customer.setEmail(email);
            }
            if (phone != null) {
                customer.setPhone(phone);
            }
            if (billingaddress != null) {
                customer.setBillingAddress(billingaddress);
            }
            customerFacade.edit(customer);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    @Interceptors(PasswordInterceptor.class)
    @Override
    public boolean loginOk(String username,String password){
        try{   
            Customer customer = customerFacade.loginOk(username, password);
            return true;
        }catch(Exception ex){
            return false;
        }
        
    }
    public List<Customer> searchbyName(String username){
        return customerFacade.findUser(username);
    }
    public double getDiscoutCustomerGroup(int id){
        return customerGroupFacade.find(id).getDiscountPercent();
        
    }

    @Override
    public Customer getCustomer(String username) {
        return customerFacade.find(username);
    }
    
}
